﻿using System.ServiceModel;
using System.IO;

namespace TfsGiggle.Core
{
    /// <summary>
    /// Do a search
    /// </summary>
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false)]
    public class Search : ISearch
    {
        /// <summary>
        /// Do a search
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        public QueryResultDTO Query(string userQuery)
        {
            if (string.IsNullOrWhiteSpace(userQuery)) return null;
            return JobManager.Instance.IndexModel.Query(userQuery);
        }

        /// <summary>
        /// Get number of documents in the index
        /// </summary>
        /// <returns></returns>
        public int GetNumberOfDocuments()
        {
            return JobManager.Instance.IndexModel.NumberOfDocuments;
        }

        /// <summary>
        /// Get this specific document from the index
        /// </summary>
        /// <param name="documentId"></param>
        /// <returns></returns>
        public QueryResultDTO GetDocument(int documentId)
        {
            return JobManager.Instance.IndexModel.GetDocument(documentId);
        }

        /// <summary>
        /// Get TfsGiggle statistics
        /// </summary>
        /// <returns></returns>
        public string GetStatistics()
        {
            return JobManager.Instance.GetStatistics();
        }

        /// <summary>
        /// Add the changeset for the specified teamprojectcollection to the index. 
        /// See example on how to find the teamprojectcollectionid
        /// <example>
        /// $tfs = [Microsoft.TeamFoundation.Client.TfsConfigurationServerFactory]::GetConfigurationServer("http://localhost:8080/tfs")
        /// $collectionService = $tfs.GetService([Microsoft.TeamFoundation.Framework.Client.ITeamProjectCollectionService])
        /// $collectionService.GetCollections()|%{ $_.Name + ": " + $_.Id }
        /// </example>
        /// </summary>
        /// <param name="teamProjectCollectionId"></param>
        /// <param name="changesetId"></param>
        public void AddChangeset(string teamProjectCollectionId, int changesetId)
        {
            JobManager.Instance.AddJob(teamProjectCollectionId, changesetId);
        }
    }
}
